#include "process.h"
#include <string.h>
#include <unistd.h>
#define SIZE 101//还要包含一个'\0'
#define STYLE '#'
//版本1：展示进度条的基本的功能
void process()
{
    char buffer[SIZE];//定义一个缓冲区
    int rate = 0;
    memset(buffer,0,sizeof(buffer));
    const char* lable = "|/-\\";
    int len = strlen(lable);

    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r",buffer,rate,lable[rate%len]);
        fflush(stdout);
        buffer[rate] = STYLE;
        rate++;
        usleep(100000);
    }
    printf("\n");
}

//版本2:根据进度动态刷新
void FlushProcess(char* tips,double total,double current)
{
    const char* lable = "|/-\\";
    int len = strlen(lable);
    static int index = 0;//设置成static变量想让他一直有效
    char buffer[SIZE];
    memset(buffer,0,sizeof(buffer));

    double rate = current*100.0/total;//比率
    int num = (int)rate;//#的个数

    for(int i=0;i<num;i++)
    {
         buffer[i] = STYLE;
    }
     printf("%s...[%-100s][%1lf%%][%c]\r",tips,buffer,rate,lable[index++]);
     fflush(stdout);
     index %= len;
    
    if(num>=100)printf("\n");
}
